package com.atguigu.realtime.app;

import com.atguigu.realtime.util.MyKafkaUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.sql.SQLException;
import java.util.*;

/**
 * @ClassName: BaseApp
 * @Description:
 * @Author: kele
 * @Date: 2021/4/17 10:06
 **/

/**
 * 定义一个baseAppV2：
 *     升级版baseApp，可以同时读取多个流，输出为Map，根据topic可以获取相应的流
 *  1、定义公共的模板，方便调用
 */
abstract public class BaseAppV2 {

    //具体的业务代码在这里执行
   protected abstract void run(StreamExecutionEnvironment env, Map<String, DataStream<String>> streams) throws SQLException, ClassNotFoundException;

   //初始化，设置执行环境，读取kafka的数据
   public void init(
           Integer port,
           Integer parallelism,
           String checkpointDir,
           String groupId,
//        至少传入一个topic
           String topic,
           String ... otherTopics) throws SQLException, ClassNotFoundException {

       System.setProperty("HADOOP_USER_NAME","atguigu");

       Configuration conf = new Configuration();
       conf.setInteger("rest.port",port);

       StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
       env.setParallelism(parallelism);

       //设置checkpoint的类型，及保存到的位置
       env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/gmall1026/" + checkpointDir));

       /**
        * 设置checkpoint
        */
       //设置严格一次
       env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

       //设置超时时间，一分钟没checkpoint认为失败
       env.getCheckpointConfig().setCheckpointTimeout(60000);

       //设置job在中止之后可以继续运行
       env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

       //设置每次只有一个checkpint执行
       env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);

       //开启checkpoint,5s进行一次checkpoint
       env.enableCheckpointing(5000);


       /**
        * 将流的结果封装为map集合，
        * key ： topic名称
        * value ： 对应的流
        */
       HashMap<String, DataStream<String>> streams = new HashMap<>();

       //1、先获取传的所有的topic
       ArrayList<String> topics = new ArrayList<>();
       topics.add(topic);
       topics.addAll(Arrays.asList(otherTopics));

       for (String t : topics) {
           DataStreamSource<String> stream = env.addSource(MyKafkaUtil.getKafkaSource(t, groupId));
           streams.put(t,stream);
       }

       run(env,streams);

       try {
           env.execute();
       } catch (Exception e) {
           e.printStackTrace();
       }

   }

}
